<template>
    <a-card  :bordered="false">
        <h2 class="text-center  margin-bottom">{{title}}</h2>
        <j-form-container>
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">
                        <a-form-item label="机构编号" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <a-input :disabled="true"  v-decorator="['customerNumber', { rules: [{ required: true, message: '机构编码不能为空' }] }]" placeholder="机构编号"  ></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机构名称" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <a-input :disabled="true"  v-decorator="['customerName', { rules: [{ required: true, message: '机构名称不能为空' }] }]" placeholder="机构名称"  ></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <a-input v-decorator="['contract', { rules: [{ required: true, message: '联系人不能为空' }] }]" placeholder="请输入联系人"  ></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <a-input v-decorator="['phone', { rules: [{ required: true, message: '联系电话不能为空' }] }]" placeholder="请输入联系电话"  ></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24" v-if="applyType == 0">
                        <a-form-item label="停业开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <j-date placeholder="请选择停业开始时间" v-decorator="['startDate']" :trigger-change="true" style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24" v-if="applyType == 0">
                        <a-form-item label="停业结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <j-date placeholder="请选择停业结束时间" v-decorator="['endDate']" :trigger-change="true" style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24" v-if="applyType == 0">
                        <a-form-item label="申请理由" :labelCol="labelCol" :wrapperCol="wrapperCol" :required="true">
                            <a-textarea  v-decorator="['applyReamrk', { rules: [{ required: true, message: '申请理由不能为空' }] }]" placeholder="请输入申请理由"  :rows="4" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24" v-else>
                        <a-form-item label="申请说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-textarea  v-decorator="['applyReamrk']" placeholder="申请说明"  :rows="4" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交 申 请</a-button>
                        <a-button style="margin-left: 20px;" @click="handleCloseCurrent">返回上一页</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-card>
</template>

<script>
    import { httpAction } from '@/api/manage'
    import { getAction } from '../../../api/manage'
    export default {
        inject:['closeCurrent'],
        data () {
            return {
                title : '机构暂停营业申请',
                applyType : 0, // 0暂停营业申请   1恢复营业申请
                customer : null,
                form: this.$form.createForm(this),
                //流程表单data
                formData : () => {},
                model: {},
                labelCol: {
                    xs: { span: 24 },
                    sm: { span: 5 },
                },
                wrapperCol: {
                    xs: { span: 24 },
                    sm: { span: 16 },
                },
                url: {
                    list: "/hc/hcSuspensionOfBusinessApply/list",
                    add: "/hc/hcSuspensionOfBusinessApply/add",
                },
            }
        },
        activated(){
            this.getInitDate()
        },
        mounted() {
            this.getInitDate()
        },
        methods: {
            getInitDate:function(){
                let params = this.$route.query;
                if(params && params.customerNumber){
                    this.customer = params;
                    if(params.applyType){
                        this.applyType = params.applyType;
                    }
                    if (this.applyType == 0) {
                        this.title = '机构暂停营业申请';
                    }
                    if (this.applyType == 1) {
                        this.title = '机构恢复营业申请';
                    }
                    this.form.setFieldsValue({
                        customerNumber : params.customerNumber,
                        customerName : params.customerName,
                        contract : params.leadername || '',
                        phone : params.leaderphone || '',
                    })
                }
            },
            getDataSource : async function(){
                let cus = this.customer;
                if(cus){
                    let params = {
                        customerNumber : cus.customerNumber,
                        applyType : this.applyType,
                        applyStatus : 0,
                    };
                    let res = await getAction(this.url.list,params);
                    if(res && res.result){
                        return res.result.records || [];
                    }
                }
                return [];
            },
           async submitForm () {
                let data = await this.getDataSource();
                console.log('data==>',data)
                if(data && data.length){
                    this.$message.info('你已有提交记录，待处理中');
                    return false;
                }

                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        let httpurl = this.url.add;
                        let method = 'post';
                        values['applyType'] = this.applyType;
                        let formData = Object.assign(this.model, values);
                        httpAction(httpurl,formData,method).then((res)=>{
                            if(res.success){
                                that.$message.success(res.message || '申请提交成功');
                                this.handleCloseCurrent()
                                this.$router.back()
                            }else{
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                        })
                    }

                })
            },
            handleCloseCurrent :function() {
                this.closeCurrent()
            }
        }
    }
</script>
